package com.skateboard.reelviewtest.view.particle;

import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Point;

import java.util.Random;

/**
 * @作者： xuze
 * @时间： 2020/11/26 4:26 PM
 * @描述： 浮点粒子
 * 背景边框 width>hight
 */
public class FloatParticle {

    private Paint mPaint;
    private Path mPath;
    private Random mRandom;

    // 圆半径
    private float mMaxRadius = 3;
    private float mRadius;
    private int mMaxAlpha=255;
    // 控件宽度
    private int mWidth;
    // 控件高度
    private int mHeight;


    private float mSpeed;
    private float mMaxSpeed = 2;
    // 火花外侧阴影大小
    private static final float BLUR_SIZE = 3.0F;

    // 路径测量
    private PathMeasure mPathMeasure;

    private float mCurDistance;

    private float mMaxDistance;


    //点
    private float mPointX, mPointY;

    public FloatParticle(int width, int height) {
        mWidth = width;
        mHeight = height;
        mRandom = new Random();


        // 抗锯齿
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        //这里是修改粒子颜色值的
        mPaint.setColor(Color.WHITE);
        // 防抖动
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.FILL);
        // 设置模糊效果 边缘模糊
        mPaint.setMaskFilter(new BlurMaskFilter(BLUR_SIZE, BlurMaskFilter.Blur.SOLID));
        //mPaint.setAlpha(mRandom.nextInt(mMaxAlpha));

        mPath = new Path();
        mPathMeasure = new PathMeasure();


        int randomValue = mRandom.nextInt(width + height);
        if (randomValue <= width) {
            mPointX = randomValue;
            mPointY = height;
        } else {
            mPointX = width;
            mPointY = width + height - randomValue;
        }

        mPath.reset();
        mPath.moveTo(mPointX, mPointY);
        if (randomValue <= width) {
            mPath.lineTo(0, mHeight - mPointX);
        } else if (randomValue <= height) {
            mPath.lineTo(0, mPointY - mWidth);
        } else {
            mPath.lineTo(mWidth - mPointY, 0);
        }
        mRadius = mRandom.nextFloat() * mMaxRadius+1;

        mSpeed = mRandom.nextFloat() * mMaxSpeed+1;

        mPathMeasure.setPath(mPath, false);

        mMaxDistance = mPathMeasure.getLength();
    }

    public void drawParticle(Canvas canvas) {
        if (mCurDistance >= mMaxDistance) {
            mCurDistance = 0;
        }
        float[] loc = new float[2];
        mPathMeasure.getPosTan(mCurDistance, loc, null);
        mPointX = loc[0];
        mPointY = loc[1];
        canvas.drawCircle(mPointX, mPointY, mRadius, mPaint);
        mCurDistance += mSpeed;
    }

}
